//
//  HExamPaperViewController.m
//  NursingManages
//
//  Created by Zhen Huang on 17/9/25.
//  Copyright © 2017年 huangzhen. All rights reserved.
//

#import "HExamPaperViewController.h"
#import "Networking.h"
#import "HCommon.h"
#import "DIHTest.h"
#import <TMCache.h>
#import "UserDefault.h"
#import "Model_Hr_TestOnLine.h"
#import "Model_Hr_ExamContent.h"
#import "HExamContentViewController.h"
#import "HExamPaperTableViewCell.h"
#import "HBaseViewController.h"
#import "InitDbTableExamOnLine.h"
#import "HHistoryExamViewController.h"
@interface HExamPaperViewController ()
<UITableViewDelegate,
UITableViewDataSource>

@property (nonatomic, strong) UITableView *tabelview;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSMutableArray *contentArray;
@property (nonatomic, assign) NSInteger rows;
@end

@implementation HExamPaperViewController

- (instancetype)init
{
    if (self) {
        self.dataArray = [NSMutableArray new];
        self.contentArray = [NSMutableArray new];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self getExamPaper];
    [self.view addSubview:self.tabelview];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    //考试上传成功,执行刷新操作
    NSNotificationCenter * centerSuccess = [NSNotificationCenter defaultCenter];
    [centerSuccess addObserver:self selector:@selector(RefreshNoticeSuccess:) name:@"examUploadSuccess" object:nil];
    
    UIBarButtonItem *execbtn = [[UIBarButtonItem alloc] initWithTitle:@"考试记录" style:UIBarButtonItemStylePlain target:self action:@selector(didDeleteButttonClicked:)];
    self.navigationItem.rightBarButtonItem = execbtn;

}

- (void)getExamPaper {
    
    
    [self.dataArray removeAllObjects];
    
    [HCommon popupMessage:@"正在请求,请稍后"];
    NSString *url = AppServiceConfig.GetServiceUrl;

    NSString *downUrl  = [NSString stringWithFormat:@"%@/%@?token=%@&userid=%@",url,@"GetExamPaperList",[[TMCache sharedCache] objectForKey:@"deviceToken"],UserDefault.getUserInfo.USER_ID];

    [Networking receiveDataWithURLString:downUrl method:@"GET" body:nil Block:^(NSData *tempData) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:tempData options:NSJSONReadingMutableContainers error:nil];
        
        BOOL flag =  [[dic valueForKey:@"flag"] boolValue];
        
        if (flag) {
            
            [HCommon dismissAnimated:YES];

        }
        
        NSDictionary *dataDic = [dic objectForKey:@"data"];
        
        NSArray *examPaperArray = [dataDic objectForKey:@"data_exam_paper"];
        
        for (NSDictionary *paperDic in examPaperArray) {
            
            Model_Hr_TestOnLine *modelOnLine = [[Model_Hr_TestOnLine alloc]initWithDictionary:paperDic];
            [self.dataArray addObject:modelOnLine];
        }
        
        [self.tabelview reloadData];

    }];
}


#pragma mark 执行刷新操作

- (void)RefreshNoticeSuccess:(NSNotification *)sender
{
    
    [self getExamPaper];
    
}

- (UITableView *)tabelview
{
    if (!_tabelview) {
        _tabelview = [[UITableView alloc]initWithFrame:CGRectMake(0, NavBarHeight, ScreenWidth, ScreenHeight - NavBarHeight) style:UITableViewStylePlain];
        _tabelview.dataSource = self;
        _tabelview.delegate = self;
        [_tabelview registerClass:[HExamPaperTableViewCell class] forCellReuseIdentifier:@"cell"];
        _tabelview.tableFooterView = [[UIView alloc]init];
        _tabelview.backgroundColor = [UIColor whiteColor];
    }
    return _tabelview;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   return  self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HExamPaperTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    Model_Hr_TestOnLine *model = self.dataArray[indexPath.row];

    cell.tempModel = model;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.rows = indexPath.row;
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"是否开始考试?"
                                                    message:nil
                                                   delegate:nil
                                          cancelButtonTitle:@"取消"
                                          otherButtonTitles:@"确定", nil];
    [alert show];
    alert.tag = 200;
    alert.delegate = self;
    

    
}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (alertView.tag == 200) {
        switch (buttonIndex) {
            case 0:{
                return;
            }
                break;
            case 1:{
                
                Model_Hr_TestOnLine *model = self.dataArray[self.rows];
                NSString *res_id = [[HBaseViewController alloc]createPrimaryID];
                HExamContentViewController *hexamContentVC = [[HExamContentViewController alloc]initWithDataArray:model.EXP_ID res_id:res_id result_flag:model.RESULT_FLAG record_flag:model.RECORD_FLAG type:0];
                NSTimeInterval interval = [model.MINUTES integerValue]  ;
                hexamContentVC.time = [NSString stringWithFormat:@"%f",interval];
                hexamContentVC.score_count = model.SCORE_COUNT ;
                hexamContentVC.title = model.EXP_NAME;
                [self.navigationController pushViewController:hexamContentVC animated:YES];


            }
                break;
            default:
                break;
        }
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 170;
}

- (void)didDeleteButttonClicked:(UIButton *)sender {
    HHistoryExamViewController *hhisVC = [[HHistoryExamViewController alloc]init];
    hhisVC.title = @"历史考试记录";
    [self.navigationController pushViewController:hhisVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
